home *** CD-ROM | disk | FTP | other *** search
- /* expback.c - try reading a file backward */
- #include "stdio.h"
- #include "cminor.h"
-
- long ftell() ;
- FILE *gfopen() ;
- FILE * fp ; /* file pointer
-
- /* modes for seek */
- #define BOF_SEEK 0
- #define REL_SEEK 1
- #define EOF_SEEK 2
-
- /* special return value for begining of file */
- #define BOF (-2)
-
- main()
- {
- int n ; /* put fread return value here */
-
- /* open the file */
- fp = gfopen("test.dat","r",BIN_MODE) ;
- printf("\n gfopen returns - %d \n",fp) ;
-
- printf("\n position char char position") ;
- printf("\n before read value after\n") ;
-
- printf("\n ") ;
- printf(" start at end of file and read backward") ;
- fseek(fp,0L,EOF_SEEK);
-
- /* read the file one character at a time */
- /* stop when we get to the beginning of the file */
-
- while( get_and_print() != BOF )
- { ; }
-
- printf("\n");
- printf("\n ") ;
- printf(" move to position %2 and read",5) ;
- fseek(fp,5L,BOF_SEEK) ;
- get_and_print() ;
-
-
- printf("\n") ;
- printf("\n ") ;
- printf(" move to position %2",5) ;
- fseek(fp,5L,BOF_SEEK) ;
- printf("\n ") ;
- printf(" then move backward 1 char and read") ;
- fseek(fp,-1L,REL_SEEK) ;
- get_and_print() ;
-
- fclose(fp) ;
- }
-
- int get_and_print()
- {
- int c ;
-
- printf("\n %21d ",ftell(fp)) ;
- c = get_previous_char() ;
- printf(" ") ;
- if( isgraphic(c) )
- printf(" %c",c) ;
- else if( c == '\n')
- printf(" LF") ;
- else if( c == '\r')
- printf(" CR") ;
- else if( c == EOF )
- printf(" EOF") ;
- else if( c == BOF )
- printf(" BOF") ;
- else if( c == 26 )
- printf("Ctl-Z") ;
- else printf(" ") ;
- printf(" %3d",c) ;
- printf(" %21d ",ftell(fp)) ;
- return(c) ;
- }
-
- int get_previous_char()
- {
- int c ;
-
- if( ftell(fp) > 0 )
- { fseek(fp,-1L,REL_SEEK) ;
- c = getc(fp) ;
- fseek(fp,-1L,REL_SEEK) ;
- return( c ) ;
- }
- else return( BOF ) ;
- }